package com.dpj.book.servlet;

import com.alibaba.fastjson.JSONObject;
import com.dpj.book.pojo.Book;
import com.dpj.book.pojo.Cart;
import com.dpj.book.pojo.CartItem;
import com.dpj.book.pojo.User;
import com.dpj.book.service.CartItemService;
import com.dpj.book.service.impl.CartItemServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/cart/*")
public class CartServlet extends BaseServlet {
    private CartItemService cartItemService = new CartItemServiceImpl();
    Integer totalCount;
    Double totalMoney;
    public void addCartItem(HttpServletRequest request, HttpServletResponse response) throws IOException {

        Integer bookId = Integer.valueOf(request.getParameter("bookId"));
        System.out.println(bookId);
        User user1 = (User) request.getSession().getAttribute("user");
        System.out.println(user1);

        CartItem cartItem = new CartItem();
        Book book = new Book(bookId);
        cartItem.setBook(book);
        cartItem.setBuyCount(1);
        cartItem.setUserBean(user1);

        //将指定的图书添加到当前用户的购物车中
        cartItemService.addOrUpdateCartItem(cartItem, user1.getCart());
    }

    public void getAllCartItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 设置响应数据的中文乱码
//        response.setContentType("text/html;charset=utf-8");
        response.setContentType("application/json;charset=utf-8");
        User user = (User) request.getSession().getAttribute("user");
        Cart cart = cartItemService.getCart(user);
        Map<Integer, CartItem> cartItemMap = cart.getCartItemMap();
        //得到购物车项数组
        List<CartItem> cartItemList = cartItemService.getCartItemList(cartItemMap);
        System.out.println(cart);
        user.setCart(cart);
        //得到购物车项总个数
        totalCount = cart.getTotalCount();
        //得到金钱总数
        totalMoney = cart.getTotalMoney();
        request.getSession().setAttribute("user", user);
        String s = JSONObject.toJSONString(cart);
        //  String totalCount1 = JSONObject.toJSONString(totalCount);
        //  String totalMoney1 = JSONObject.toJSONString(totalMoney);
        String s1 = JSONObject.toJSONString(cartItemList);

         response.getWriter().write(s1);

    }

    public void getTotal(HttpServletRequest request, HttpServletResponse response) throws IOException {

          String totalCount1 = JSONObject.toJSONString(totalCount);
          String totalMoney1 = JSONObject.toJSONString(totalMoney);

        String s = "{\"code\":\"success\",\"totalCount\":\"" + totalCount1 + "\",\"totalMoney\":\"" + totalMoney1 + "\"}";
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().print(s);
    }

    public void editCartItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        Integer buyCount = Integer.valueOf(request.getParameter("buyCount"));
        CartItem cartItem=new CartItem();
        cartItem.setId(id);
        cartItem.setBuyCount(buyCount);
        cartItemService.updateCartItem(cartItem);

    }


}
